package app;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.menhoo.toollibrary.common.assist.Check;
import com.example.menhoo.toollibrary.common.utils.BitmapUtil;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import application.HttpUrlConstant;
import interfaces.IReturnObject;
import mainUI.AppActivity;
import menhoo.com.i8officelibrary.R;
import model.AppModel;

/**
 * 全部应用
 * Created by yanglei on 16/1/10.
 */

public class AllApp extends AppActivity {

    ListView listView;

    Button button;

    MyListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_app);
        try{
            listView = (ListView)findViewById(R.id.listView);
            button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(AllApp.this, AppSet.class));
                }
            });
        }
        catch (Exception e){
            LogUtils.e(e.getMessage()+"");
        }

        setTitle("全部应用");

        listView.setEmptyView(findViewById(R.id.linearLayoutEmpty));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final AppModel item = (AppModel) parent.getAdapter().getItem(position);
                if (item != null) {
                    Intent it = new Intent(AllApp.this, AutoFragment.class);
                    it.putExtra("Model", item);
                    startActivity(it);
                }
            }
        });

        hideContent();
        requestData();
    }


    /**
     * 请求所有app数据
     */
    private void requestData(){
        Post(HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.GetApps), new IReturnObject<String>() {
            @Override
            public void returnResult(String result) {
                if (result != null) {
                    try {
                        System.out.println(result);
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getBoolean("Succeed")) {

                            List<AppModel> list = new ArrayList<AppModel>();
                            JSONArray appArray = jsonObject.getJSONArray("Data");
                            for (int i = 0; i < appArray.length(); i++) {
                                JSONObject appJson = appArray.getJSONObject(i);
                                String Code = appJson.getString("Code");
                                String Name = appJson.getString("Name");
                                String Type = appJson.getString("Type");
                                String Icon = appJson.getString("Icon");
                                String Url = appJson.getString("Url");

                                //2018-4-24
                                // 将对话去掉
                                if(Code.equalsIgnoreCase("IM")){
                                    continue;
                                }

                                AppModel model = new AppModel();
                                model.code = Code;
                                model.type = Type;
                                model.name = Name;
                                model.htmlUrl = Url;
                                model.iconUrl = HttpUrlConstant.getWebSiteFullUrl(Icon);

                                list.add(model);
                            }

                            adapter = new MyListViewAdapter(list);
                        }
                    } catch (Exception e) {
                        LogUtils.e(e.getMessage() + "");
                    }
                }
                if(adapter!=null && adapter.getCount()>0){
                    listView.setAdapter(adapter);
                    button.setVisibility(View.VISIBLE);
                }
                else{
                    button.setVisibility(View.GONE);
                }
                showContent();
            }
        });
    }

    class MyListViewAdapter extends BaseAdapter {
        List<AppModel> data = new ArrayList();

        public MyListViewAdapter(List<AppModel> list){
            data = list;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public AppModel getItem(int position) {
            return data.size()>0? data.get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder=null;

            if(convertView==null){
                viewHolder = new ViewHolder();
                convertView= getLayoutInflater().inflate(R.layout.adapter_all_app_item, null);
                viewHolder.textViewTitle=(TextView)convertView.findViewById(R.id.textViewTitle);
                viewHolder.textViewVersion=(TextView)convertView.findViewById(R.id.textViewVersion);
                viewHolder.textViewSize=(TextView)convertView.findViewById(R.id.textViewSize);
                viewHolder.textViewDesc=(TextView)convertView.findViewById(R.id.textViewDesc);
                viewHolder.imageView=(ImageView)convertView.findViewById(R.id.imageView);
                convertView.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            AppModel model = getItem(position);

            viewHolder.textViewTitle.setText(model.name);
            viewHolder.textViewVersion.setText(model.version);
            viewHolder.textViewSize.setText(model.size);
            viewHolder.textViewDesc.setText(model.desc);

            if(Check.isEmpty(model.iconUrl)){
                viewHolder.imageView.setImageResource(model.icon);
            }
            else{
                getIconLoader().display(viewHolder.imageView, model.iconUrl, new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        imageView.setImageBitmap(BitmapUtil.getRoundedCornerBitmap(bitmap,10f));
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                        imageView.setImageResource(R.drawable.load_failed);
                    }
                });
            }

            return convertView;
        }
    }

    class ViewHolder{
        TextView textViewTitle,textViewVersion,textViewSize,textViewDesc;
        ImageView imageView;
    }
}
